﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using MySql.Data.MySqlClient;

public class DivisionBLL
{
    string connection = WebConfigurationManager.ConnectionStrings["UCNLinkDB"].ConnectionString;

	public DivisonProperty GetDivisionByName(string divName)
	{
        MySqlConnection conn = new MySqlConnection(connection);
        MySqlCommand cmd = new MySqlCommand("SELECT tblDivision.ID, tblDivision.divisionName, tblDivision.divisionDescription FROM tblDivision WHERE divisionName = @divisionName", conn);

        cmd.Parameters.Add("@divisionName", MySqlDbType.VarChar, 50);
        cmd.Parameters["@divisionName"].Value = divName;

        conn.Open();

        cmd.Connection = conn;

        DivisonProperty divProp = new DivisonProperty();

        MySqlDataReader reader = cmd.ExecuteReader();

        if (reader.Read())
        {
            divProp.Id = Convert.ToInt32(reader["ID"]);
            divProp.DivisionName = reader["divisionName"].ToString();
            divProp.DivisionDiscription = reader["divisionDescription"].ToString();

            reader.Close();
        }
        cmd.Dispose();
        conn.Close();

        return divProp;
	}

    public DivisonProperty GetDivisionByID(int divId)
    {
        MySqlConnection conn = new MySqlConnection(connection);
        MySqlCommand cmd = new MySqlCommand("SELECT tblDivision.ID, tblDivision.divisionName, tblDivision.divisionDescription FROM tblDivision WHERE ID = @ID", conn);

        cmd.Parameters.Add("@ID", MySqlDbType.Int32);
        cmd.Parameters["@ID"].Value = divId;

        conn.Open();

        cmd.Connection = conn;

        DivisonProperty divProp = new DivisonProperty();

        MySqlDataReader reader = cmd.ExecuteReader();
        if (reader.Read())
        {
            divProp.Id = Convert.ToInt32(reader["ID"]);
            divProp.DivisionName = reader["divisionName"].ToString();
            divProp.DivisionDiscription = reader["divisionDescription"].ToString();

            reader.Close();
        }
        cmd.Dispose();
        conn.Close();

        return divProp;
    }

    public List<DivisonProperty> GetAllDivision()
    {
        MySqlConnection conn = new MySqlConnection(connection);
        MySqlCommand cmd = new MySqlCommand("SELECT tblDivision.ID, tblDivision.divisionName, tblDivision.divisionDescription FROM tblDivision", conn);

        List<DivisonProperty> ldp = new List<DivisonProperty>();

        conn.Open();

        cmd.Connection = conn;

        DivisonProperty divProp = new DivisonProperty();

        MySqlDataReader reader = cmd.ExecuteReader();
        while (reader.HasRows)
        {
            divProp.Id = Convert.ToInt32(reader["ID"]);
            divProp.DivisionName = reader["divisionName"].ToString();
            divProp.DivisionDiscription = reader["divisionDescription"].ToString();
            ldp.Add(divProp);
            reader.Close();
        }
        cmd.Dispose();
        conn.Close();

        return ldp;
    }

    public DivisionBLL()
    {
    }
}